package com.iotboss.web.controller.system;

import com.iotboss.common.annotation.Log;
import com.iotboss.common.core.controller.BaseController;
import com.iotboss.common.core.domain.AjaxResult;
import com.iotboss.common.core.domain.entity.SysDept;
import com.iotboss.common.core.domain.iot.IotCustom;
import com.iotboss.common.core.domain.iot.IotDeptCompany;
import com.iotboss.common.core.domain.iot.IotOrUser;
import com.iotboss.common.core.page.TableDataInfo;
import com.iotboss.common.enums.BusinessType;
import com.iotboss.common.utils.SecurityUtils;
import com.iotboss.common.utils.StringUtils;
import com.iotboss.system.service.ISysDeptService;
import com.iotboss.system.service.IotCustomService;
import com.iotboss.system.service.IotDeptCompanyService;
import com.iotboss.system.service.IotOrUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/iotCustom")
public class IotCustomController extends BaseController {
    @Autowired
    private IotCustomService customService;
    @Autowired
    private IotOrUserService iotOrUserService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IotDeptCompanyService deptCompanyService;

    /**
     * 查询客户列表
     *
     * @param iotCustom
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:iotCustom:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotCustom iotCustom) {
        startPage();
        List<IotCustom> list = customService.selectIotCustomList(iotCustom);
        return getDataTable(list);
    }

    /**
     * 根据客户编号获取客户信息
     */
    @PreAuthorize("@ss.hasPermi('system:iotCustom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(customService.selectIotCustomById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('system:iotCustom:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody IotCustom iotCustom) {
        if (!customService.findIotCustom(iotCustom)) {
            return error("新增客户失败，企业客户已存在");
        }
        iotCustom.setCreateBy(getUsername());
        return toAjax(customService.insertIotCustom(iotCustom));
    }

    /**
     * 新增客户下的公司
     */
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addCompany")
    public AjaxResult addCompany(@Validated @RequestBody IotCustom iotCustom) {
        SysDept sysDept = new SysDept();
        sysDept.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        IotCustom iotCustom1 = iotOrUserService.findIotCustom(iotCustom.getId());
        if (StringUtils.isNotEmpty(iotCustom1.getDeptId().toString())) {
            SysDept deptById = deptService.selectDeptById(iotCustom1.getDeptId());
            sysDept.setParentId(deptById.getParentId());
        }
        sysDept.setDeptName(iotCustom.getCompanyName());
        sysDept.setPhone(iotCustom.getCustomPhone());
        sysDept.setLeader(iotCustom.getCustomName());
        int i = customService.addCompany(sysDept);
        this.cusToCompany(iotCustom,sysDept);
        return toAjax(i);
    }

    private void cusToCompany(IotCustom iotCustom, SysDept sysDept) {
        IotDeptCompany company = new IotDeptCompany();
        company.setCompanyName(iotCustom.getCompanyName());
        company.setCustomId(iotCustom.getId());
        company.setDeptId(sysDept.getDeptId());
        company.setCreateBy(getUsername());
        deptCompanyService.insertCompany(company);
        //添加操作记录
        IotOrUser iotOrUser = new IotOrUser();
        iotOrUser.setDeptId(company.getDeptId());
        iotOrUser.setCreateBy(getUsername());
        iotOrUser.setType(0);
        iotOrUser.setCustomId(iotCustom.getId());
        iotOrUserService.insertOrUser(iotOrUser);
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('system:iotCustom:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody IotCustom iotCustom) {
        iotCustom.setUpdateBy(getUsername());
        int i = customService.updateIotCustom(iotCustom);
        if (iotCustom.getId() != null) {
            //返回DeptId
            IotCustom iotCustom1 = iotOrUserService.findIotCustom(iotCustom.getId());
            IotOrUser iotOrUser = new IotOrUser();
            iotOrUser.setType(1);
            if (StringUtils.isNotEmpty(iotCustom1.getDeptId().toString())) {
                iotOrUser.setDeptId(iotCustom1.getDeptId());
            }
            iotOrUser.setCustomId(iotCustom.getId());
            iotOrUser.setCreateBy(getUsername());
            iotOrUserService.insertOrUser(iotOrUser);
        }
        return toAjax(i);
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('system:iotCustom:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long[] id) {
        for (Long aLong : id) {
            if (aLong != null) {
                IotCustom iotCustom1 = iotOrUserService.findIotCustom(aLong);
                IotOrUser iotOrUser = new IotOrUser();
                iotOrUser.setType(2);
                iotOrUser.setDeptId(iotCustom1.getDeptId());
                iotOrUser.setCustomId(aLong);
                iotOrUser.setCreateBy(getUsername());
                iotOrUserService.insertOrUser(iotOrUser);
            }
        }
        return toAjax(customService.deleteIotCustomByIds(id));
    }
}
